//
//  messageClient.m
//  01_kakin
//
//  Created by jon due on 12/03/28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "messageClient.h"

@implementation messageClient


-(NSArray*)selectGeneral:(NSString*)sql{
    
    NSMutableArray *array = [[ NSMutableArray alloc ]init];
    if (status == DBStatusClosed) {
		[super open];
	}
    FMResultSet *rs = [innerDB executeQuery:sql];
	messageBean *Bean = nil;
	while ([rs next]) {
		Bean = [[messageBean alloc] init];
        
        Bean.No =           [ rs intForColumn: MS_NO ];
        Bean.version =      [ rs stringForColumn:MS_VERSION ];
        Bean.message =      [ rs stringForColumn:MS_MESSAGE ];
                
        [array addObject:Bean];
		[Bean release];
	}
    
	[rs close];
    
	if ([innerDB hadError]) {
        NSLog(@"database path: %@", [innerDB databasePath]);
		NSLog(@"database error: %@", [innerDB lastErrorMessage]);
	}
	
	return [array autorelease];

}

-(NSArray*)allMessage{
    NSString *sqlQuery = 
	[NSString stringWithFormat:@"SELECT * FROM %@", TB_MESSAGE];
    
    return [ self selectGeneral:sqlQuery ];

}

-(void)insertMessage:(messageBean*)Bean{
    
    NSString *sql = [NSString stringWithFormat:
                     @"INSERT INTO %@ (%@,%@) VALUES('%@','%@')",
                     TB_MESSAGE,
                     MS_VERSION,
                     MS_MESSAGE,
                     Bean.version,
                     Bean.message];
    
	[super open];
    [ innerDB executeUpdate:sql ];
    
	if ([innerDB hadError]) {
		NSLog(@"database error: %@", [innerDB lastErrorMessage]);
	}
	[super close];

}

@end
